package Homework.cms.servlet;


import Homework.cms.user.Member;
import Homework.cms.user.MemberService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");     //告诉它当前文档的编码
        response.setHeader("Content-Type","text/html;utf-8");    //告诉浏览器文档类型
        String telephone=request.getParameter("phone");
        String password=request.getParameter("pwd");
        String truename=request.getParameter("truename");
        String email=request.getParameter("email");

        String vali1=request.getParameter("vali");                          //这是用户输入的验证码
        String vali=request.getSession().getAttribute("identify").toString();   //这是图片里验证码的内容

        PrintWriter out=response.getWriter();   //获取一个打印流，向网页输出打印
        int error=0;
        if(vali1.equals(vali)){

            if(!valiphone(telephone)==true){
                out.println("手机号格式不符合标准，请重新输入！");
                out.println("<br>");
                error++;
            }
            if(!valiPwd(password)==true){
                out.println("密码格式不符合标准，请重新输入！");
                out.println("<br>");
                error++;
            }
            if(!valiTrueName(truename)==true){
                out.println("真实姓名格式不符合标准，请重新输入！");
                out.println("<br>");
                error++;
            }
            if(!valiemail(email)==true){
                out.println("邮箱格式不符合标准，请重新填写！");
                out.println("<br>");
                error++;
            }
            if(error==0){   //没有错误信息
                MemberService memberService=new MemberService();
                if(memberService.check(telephone)==false){
                    //out.println("注册成功！");
                    Member member=new Member();
                    member.setTelephone(telephone);
                    member.setPassword(password);
                    member.setTruename(truename);
                    member.setEmail(email);

                    if(!memberService.register(member)){
                        out.println("注册失败！");
                    }
                    else {
                        out.println("注册成功！3秒后跳转到登录界面。");
                        response.setHeader("refresh", "3;url=/webtest/Homework/login.jsp");
                    }
                }
                else {
                    out.println("用户名已存在！注册失败");
                }
            }
            else {
                out.println("注册失败！");
            }
        }
        else {
            out.println("验证码错误！注册失败！");
            out.println("<br>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    public static boolean valiphone(String phone){
        String regEx1="[0-9]{11}+";
        Pattern p1=Pattern.compile(regEx1);
        Matcher m1=p1.matcher(phone);
        if(m1.find()){
            return true;
        }
        else {
            return false;
        }
    }

    public static boolean valiPwd(String pwd){
        if(pwd.length()<6 || pwd.length()>15){   //&lt;是小于号的写法
            return false;
        }
        else {
            return true;
        }
    }

    public static boolean valiTrueName(String name){
        String regEx1="[\\u4E00-\\u9FA5]{2,10}+";
        Pattern p1=Pattern.compile(regEx1);
        Matcher m1=p1.matcher(name);
        if(m1.find()){
            return true;
        }
        else {
            return false;
        }
    }

    public static boolean valiemail(String email){
        //var form=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;      //电子邮箱的正则表达式
        String regEx1="[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\\.)+(com|cn|net|org)+";
        Pattern p1=Pattern.compile(regEx1);
        Matcher m1=p1.matcher(email);
        if(m1.find()){
            return true;
        }
        else {
            return false;
        }
    }
}
